package com.sophossystems.client.asmon.service.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

import com.sophossystems.client.asmon.dao.UserDao;
import com.sophossystems.client.asmon.dto.UserDTO;
import com.sophossystems.client.asmon.dto.UserPkDTO;
import com.sophossystems.client.asmon.exceptions.BusinessException;
import com.sophossystems.client.asmon.service.LoginService;

public class LoginServiceImpl implements LoginService {

	private UserDao userDao;
	
	
	private static Logger log = Logger.getLogger(LoginServiceImpl.class);
	@Override
	public UserDTO login(UserDTO user) throws BusinessException {
		UserDTO userdto = null;
		String md5Password = null;
		try {
//			md5Password = md5Support(user.getPassword());
			log.debug("LoginServiceImpl: "+ user.getUsername().toString()+".  El usuario: "+user.getUsername()+" EL password: "+user.getPassword());
			userdto = userDao.login(user.getUsername().toString(), user.getPassword());
		} catch (Exception ex) {
			log.debug ("Error login: "+ex.getCause() );
			throw new BusinessException("Error logearse " + user.getUsername()+" / "
					+ ex.getMessage());
			
		}
		return userdto;
	}

	public void setUserDao(UserDao usuario) {
		this.userDao= usuario;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDaoo(UserDao userDao) {
		this.userDao = userDao;
	}

	private String md5Support(String texto) throws NoSuchAlgorithmException {
		String regreso = null;
		MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
		digest.update(texto.getBytes());

		byte[] hash = digest.digest();
		regreso = hash.toString();
		return regreso;

	}

	@Override
	public UserDTO addUser(UserDTO user) throws BusinessException {
		UserDTO usuariodto = null;
		log.debug("LoginService.addUser - En el metodo");
		try {
			if (userDao.findWhereUsernameEquals(user.getUsername()) == null) {
				log.debug("LoginServicesImpl. addUser. No se encontro el usuario. Se insertará el usuario");
				user.setPassword(this.md5Support(user.getPassword()));
				userDao.insert(user);
				usuariodto = userDao.findWhereUsernameEquals(user.getUsername()).get(0);
			}else 
			{
				return null;
			}
		} catch (Exception ex) {
			throw new BusinessException("Error añadir usuario"
					+ ex.getMessage());
		}

		return usuariodto;
	}

	@Override
	public UserDTO modifyUser(UserDTO user) throws BusinessException {
		UserDTO usuariodto = null;
		try {
			UserDTO userTemp = userDao.findWhereUsernameEquals(user.getUsername()).get(0);
			if (userDao.findWhereUsernameEquals(user.getUsername()) != null) {
				user.setIdUser(userTemp.getIdUser());
				UserPkDTO pk = new UserPkDTO();
				pk.setIdUser(user.getIdUser());
				userDao.update( user);
				
				usuariodto = userDao.findWhereUsernameEquals(user.getUsername()).get(0);
			}else 
			{
				return null;
			}
		} catch (Exception ex) {
			throw new BusinessException("Error añadir usuario"
					+ ex.getMessage());
		}

		return usuariodto;
	}

}
